package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.ui.activity.DeviceGroupListActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DeviceGroupListAdapter  extends AppAdapter<ControllerBean> {
    private Map<String, DeviceBean> mSelected = new LinkedHashMap<>();
    private boolean isSelectable = false;
    private boolean isSingleSelect = false;
    public int adapterType;
    public DeviceGroupListAdapter(Context context) {
        super(context);
    }

    public DeviceGroupListAdapter(@NonNull @NotNull Context context, boolean isSelectable, boolean isSingleSelect) {
        super(context);
        this.isSelectable = isSelectable;
        this.isSingleSelect = isSingleSelect;
    }

    public Map<String, DeviceBean> getSelectMap(){
        return mSelected;
    }

    public void initSelectMap(Map<String, DeviceBean> data){
        mSelected = data;
    }

    public List<DeviceBean> getSelectedData() {
        List<DeviceBean> checkList = new ArrayList<>();
        if (mSelected == null) return checkList;
        for (String key : mSelected.keySet()) {
            checkList.add(mSelected.get(key));
        }
        return checkList;
    }

    @NonNull
    @Override
    public DeviceGroupListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DeviceGroupListAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvCoorName;
        private final TextView tvGroupNum;
        private final RecyclerView roomRecyclerView;
        private final ImageView imgAdd;
        private DeviceGroupAdapter deviceGroupAdapter = null;
        private GroupEnrollmentAdapter groupEnrollmentAdapter = null;
        private final View btnAddGroup;

        private ViewHolder() {
            super(R.layout.device_group_list_item);
            tvCoorName = findViewById(R.id.tv_group_name);
            tvGroupNum = findViewById(R.id.tv_group_number);
            roomRecyclerView = findViewById(R.id.room_recycler_view);
            imgAdd = findViewById(R.id.add_group);
            btnAddGroup = findViewById(R.id.btn_add_group);
            roomRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            if(isSelectable){
                groupEnrollmentAdapter = new GroupEnrollmentAdapter(getContext()){
                    @Override
                    public void onSelectChanged(Map<String, DeviceBean> selected) {
                        super.onSelectChanged(selected);
                        DeviceGroupListAdapter.this.onSelectChanged(selected);
                    }
                };
                groupEnrollmentAdapter.setCanBeEmpty(false);
                groupEnrollmentAdapter.setSingleSelect(isSingleSelect);
                roomRecyclerView.setAdapter(groupEnrollmentAdapter);
            }else{
                deviceGroupAdapter = new DeviceGroupAdapter(getContext()){
                    @Override
                    public void onSelectChanged(Map<String, DeviceBean> selected) {
                        super.onSelectChanged(selected);
                        DeviceGroupListAdapter.this.onSelectChanged(selected);
                    }
                };
                deviceGroupAdapter.setCanBeEmpty(false);
                deviceGroupAdapter.setSingleSelect(isSingleSelect);
                roomRecyclerView.setAdapter(deviceGroupAdapter);
            }

        }


        @Override
        public void onBindView(int position) {
            ControllerBean controllerBean = getItem(position);
            if(controllerBean.getDeviceItems().size() == 0){
                imgAdd.setVisibility(View.GONE);
                roomRecyclerView.setVisibility(View.GONE);
                if(adapterType == 1){
                    btnAddGroup.setVisibility(View.VISIBLE);
                }
            }else{
                btnAddGroup.setVisibility(View.GONE);
                roomRecyclerView.setVisibility(View.VISIBLE);
                if(adapterType == 1){
                    imgAdd.setVisibility(View.VISIBLE);
                }
            }
            tvCoorName.setVisibility(View.VISIBLE);
            tvCoorName.setText(controllerBean.getName());
            tvGroupNum.setText("("+ controllerBean.getDeviceItems().size() +"/16)");
            if(isSelectable){
                groupEnrollmentAdapter.setSelected(mSelected);
                groupEnrollmentAdapter.setCanBeEmpty(false);
                groupEnrollmentAdapter.setData(controllerBean.getDeviceItems());
            }else{
                deviceGroupAdapter.setSelected(mSelected);
                deviceGroupAdapter.setCanBeEmpty(false);
                deviceGroupAdapter.setData(controllerBean.getDeviceItems());
                deviceGroupAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(RecyclerView recyclerView, View itemView, int devicePosition) {
                        if(adapterType == 1){
                            ((DeviceGroupListActivity)getContext()).gotoGroupSettingVIew(controllerBean.getDeviceItems().get(devicePosition));
                        }else{
                            ((DeviceGroupListActivity)getContext()).gotoGroupAddView(position,devicePosition);
                        }
                    }
                } );
            }
        }
    }

    public void clearSelected(){
        mSelected.clear();
        notifyDataSetChanged();
    }

    public void changeIsSingleSelect(boolean value){
        isSingleSelect = value;
        notifyDataSetChanged();
    }

    //外部实现
    public void onSelectChanged(Map<String, DeviceBean> selectBean){
        notifyDataSetChanged();
    }


}
